package sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber;


import rx.Subscriber;
import sdk.webview.fmc.com.fmcsdk.base.BaseView;

/**
 * 请使用此类来subscribe Retrofit返回Observable.
 *
 * <P>
 * 此类会判断网络错误与业务错误, 并分发给{@link #error(Throwable)}
 * 与{@link # operationError(Object, int, String)}函数. 当请求成功同时业务成功的情况下会调用
 * {@link #success(Object)}函数. 如果在创建ResponseSubscriber对象的同时传入MVPView对象,
 * 此类会托管隐藏加载框与错误处理, 如果希望自行处理错误, 请覆写{@link #error(Throwable)}函数,
 * 并且返回true.
 *
 */
public abstract class ResponseSubscriber<T> extends Subscriber<T>
        implements ResponseHandler.CustomHandler<T> {

    private ResponseHandler<T> handler;

    public ResponseSubscriber() {
        handler = new ResponseHandler<>(this);
    }

    public ResponseSubscriber(BaseView view) {
        handler = new ResponseHandler<>(this, view);
    }

    @Override
    public void onCompleted() {
        handler.onCompleted();
        handler = null;
    }

    @Override
    public void onError(Throwable e) {
        handler.onError(e);
        handler = null;
    }

    @Override
    public void onNext(T t) {

        handler.onNext(t);
    }

    public abstract void onRequestAgain();

    @Override
    public abstract void success(T t);

    @Override
    public boolean operationError(T t, String status, String message) {
        return false;
    }

    @Override
    public boolean error(Throwable e) {
        return false;
    }

    /***
     * token失效后再次获取token,并在此方法内重新调用该接口
     */
    @Override
    public void requestAgain() {
        onRequestAgain();
    }


}
